{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c0753e1d",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# 制作手机铃声\n",
    "\n",
    "\n",
    "<font style=\"color:red;font-size:20px\">实际上就是截取音频中精彩的一部分</font>\n",
    "\n",
    "<img src=\"images/trim_audio.jpg\" width=\"50%\" align=\"left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3834bf26",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 1、导入包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc798c3b",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "from IPython.display import * \n",
    "from av_utils import av_trim\n",
    "import os"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b59c1d83",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 2、准备一个音频文件（mp3）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d91675b4",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "input_file = 'audio/少年.mp3'  # 原始音频文件\n",
    "\n",
    "print(\"原始音频路径：\", os.path.abspath(input_file))\n",
    "\n",
    "# 听原始音频，找到精彩开始和结束的时间点\n",
    "Audio(input_file, autoplay=True)   "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0fda09ec",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 3、开始截取制作铃声"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ff25ba4",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 指定开始和结束的时间点\n",
    "# 时间格式为：时:分:秒(01:50:30，01:50:30.12)，分:秒（30:20、30:20.15）\n",
    "start_time = '01:03.2' # 截取开始时间点为 1 分钟 3.2 秒\n",
    "end_time = '01:15.0'   # 截取结束时间点为 1 分钟 15 秒\n",
    "\n",
    "# 输出的手机铃声音频文件名\n",
    "output_file = 'output/少年_铃声.mp3'\n",
    "\n",
    "print(\"手机铃声路径：\", os.path.abspath(output_file))\n",
    "\n",
    "# 开始制作铃声\n",
    "av_trim(input_file=input_file,\n",
    "           start_time=start_time, end_time=end_time,\n",
    "           output_file=output_file)\n",
    "# 听听铃声效果\n",
    "Audio(output_file, autoplay=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21362ba5",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "<font style=\"color:red;font-size:20px\">如果感觉铃声声音太大或者太小</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "acecc027",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 4、调整铃声的音量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "065ff834",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "volume = 1.0 # 声音音量不变\n",
    "volume = 2.0 # 声音音量增大2倍\n",
    "volume = 0.5 # 声音音量减小2倍\n",
    "\n",
    "print(\"手机铃声路径：\", os.path.abspath(output_file))\n",
    "\n",
    "# 开始制作铃声\n",
    "av_trim(input_file=input_file,\n",
    "           start_time=start_time, end_time=end_time,\n",
    "           output_file=output_file,\n",
    "           volume=volume)\n",
    "\n",
    "# 听听铃声效果\n",
    "Audio(output_file, autoplay=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "204c545c",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "<font style=\"color:red;font-size:20px\">如果感觉铃声开始太突然了，我们可以加入声音淡入效果</font>\n",
    "\n",
    "<img src=\"images/fade_in_out.jpg\" width=\"50%\" align=\"left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab1361fe",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 5、调整铃声的淡入淡出效果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25f07425",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# 在音频开始的 3 秒期间，音量从无逐渐增大（如果不需要，可以等于0）\n",
    "fade_in_duration = 3 \n",
    "# 在音频结束的 5 秒期间，音量逐渐减小到无（如果不需要，可以等于0）\n",
    "fade_out_duration = 5\n",
    "# 声音音量不变\n",
    "volume = 1.0\n",
    "\n",
    "print(\"手机铃声路径：\", os.path.abspath(output_file))\n",
    "\n",
    "# 开始制作铃声\n",
    "av_trim(input_file=input_file,\n",
    "        start_time=start_time, \n",
    "        end_time=end_time,\n",
    "        output_file=output_file,\n",
    "        volume=volume,\n",
    "        fade_in_duration=fade_in_duration, \n",
    "        fade_out_duration=fade_out_duration)\n",
    "\n",
    "# 听听效果\n",
    "Audio(output_file, autoplay=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}